﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GestorSQL.BLL;
using GestorSQL.Security.Interface;
using GestorSQL.BusinessEntities;
using System.Threading;

namespace GestorSQL.GUI
{
    public partial class ConnectionWindow : Form
    {
        private ConnectionManager _connManager = new ConnectionManager();

        public Conexion Connection { get; private set; }

        #region constructor
        public ConnectionWindow(bool showOnlyProduction)
        {
            InitializeComponent();
            SetDdlAmbientes(showOnlyProduction);
        }



        #endregion

        #region metodos privados
        private void SetDdlAmbientes(bool showOnlyProduction)
        {
            CustomPrincipal pr = (CustomPrincipal)Thread.CurrentPrincipal;
            List<Ambiente> ambientes = _connManager.GetEnvironmentList(pr.Roles);
            List<String> servidores = null;
            
            if (showOnlyProduction)
                ambientes = new List<Ambiente>() { Ambiente.Produccion };


            ddlCore.DataSource = _connManager.GetCoreList();
            ddlAmbiente.DataSource = ambientes;

            servidores = _connManager.GetServerListByEnvironment(ambientes.FirstOrDefault());
            ddlServidor.DataSource = servidores;
            ddlConexion.DataSource = _connManager.GetConnectionListByServerAndEnvironment(ambientes.FirstOrDefault(), servidores.FirstOrDefault());

            ddlCore.SelectedValue = _connManager.GetConnection(ddlConexion.SelectedValue.ToString()).Fk_Core;
        }

        private bool ValidateForm()
        {
            if (ddlConexion.SelectedItem != null)
            {
                Connection = _connManager.GetConnection(ddlConexion.SelectedItem.ToString());
                if (Connection != null)
                    return true;
            }
            return false;
        }

        #endregion

        #region handlers

        private void ddlAmbiente_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<string> servidores = _connManager
                .GetServerListByEnvironment((Ambiente)ddlAmbiente.SelectedItem);
            ddlServidor.DataSource = servidores;

            ddlConexion.DataSource = _connManager.GetConnectionListByServerAndEnvironment((Ambiente)ddlAmbiente.SelectedItem, servidores.FirstOrDefault());

            if (ddlConexion.SelectedValue != null)
                ddlCore.SelectedValue = _connManager.GetConnection(ddlConexion.SelectedValue.ToString()).Fk_Core;

        }

        private void ddlServidor_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (ddlAmbiente.SelectedItem != null)
            {
                List<string> conexiones = _connManager
                    .GetConnectionListByServerAndEnvironment
                    ((Ambiente)ddlAmbiente.SelectedItem,
                    ddlServidor.SelectedItem.ToString());

                ddlConexion.DataSource = conexiones;

                ddlCore.SelectedValue = _connManager.GetConnection(ddlConexion.SelectedValue.ToString()).Fk_Core;

            }
        }

        private void ddlCore_SelectedIndexChanged(object sender, EventArgs e)
        {
           //Obtener los servidores de acuerdo al core seleccionado.
            ddlServidor.DataSource = _connManager.GetServerListByCore(Int32.Parse(ddlCore.SelectedValue.ToString()));
        }

        private void btAceptar_Click(object sender, EventArgs e)
        {
            if (!ValidateForm())
                return;
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btCancelar_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
        #endregion

    }
}
